#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

int main(int argc,char* argv[])
{
	if(argc != 3)
	{
		perror("argc");
		return 1;
	}

	int sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(sockfd<0)
	{
		perror("socket");
		return 2;
	}

	char buf[128];
	struct sockaddr_in server;
	server.sin_family=AF_INET;
	server.sin_port=htons(atoi(argv[2]));
	server.sin_addr.s_addr=inet_addr(argv[1]);

	if(connect(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
	{
		perror("connect");
		return 3;
	}
	printf("connect success...\n");

	while(1)
	{
		ssize_t r=read(0,buf,sizeof(buf));
		if(r<0)
		{
			printf("read failed\n");
			continue;
		}
		buf[r-1]=0;

		send(sockfd,buf,strlen(buf),0);
		ssize_t rec=recv(sockfd,buf,sizeof(buf),0);
		buf[rec]=0;
		printf("server > %s\n",buf);
	}
	close(sockfd);

	return 0;
}
